﻿Public Class TMyInnoDBStatus
    Private Enum TRowStatus As Integer
        rsNotIn = 0
        rsIn1 = 1
        rsIn2 = 2
        rsOut = 3
    End Enum
    Private _MyQuery As New TMyQuery
    Private _RawResult As New List(Of String)
    Private _BackGroundThread As New List(Of String)
    Private _Semaphores As New List(Of String)
    Private _Transactions As New List(Of String)
    Private _FileIO As New List(Of String)
    Private _InsertBufferAndAdaptiveHashIndex As New List(Of String)
    Private _Log As New List(Of String)
    Private _BufferPoolAndMemory As New List(Of String)
    Private _RowOperations As New List(Of String)

    Public Sub New(ByVal AConnection As TMySession)
        _MyQuery.Session = AConnection
    End Sub

    Public Sub Refresh()
        _RawResult.Clear()
        _MyQuery.Clear()
        _MyQuery.SQL.AppendLine("show engine innodb status")
        _MyQuery.Execute()
        If Not _MyQuery.Eof Then
            Dim result As String = _MyQuery.FieldAsString("status")
            Dim sTmp As String() = result.Split(ControlChars.Lf)
            For Each s As String In sTmp
                _RawResult.Add(s)
            Next
        End If
        _MyQuery.Close()
        SetResult()
    End Sub
    Private Sub Increment(ByRef ARowStatus As TRowStatus)
        If ARowStatus = TRowStatus.rsIn1 Then
            ARowStatus = TRowStatus.rsIn2
        ElseIf ARowStatus = TRowStatus.rsIn2 Then
            ARowStatus = TRowStatus.rsOut
        End If
    End Sub
    Private Sub SetResult()
        Dim startBackGroundThread As TRowStatus = TRowStatus.rsNotIn
        Dim startSemaphores As TRowStatus = TRowStatus.rsNotIn
        Dim startTransactions As TRowStatus = TRowStatus.rsNotIn
        Dim startFileIO As TRowStatus = TRowStatus.rsNotIn
        Dim startInsertBufferAndAdaptiveHashIndex As TRowStatus = TRowStatus.rsNotIn
        Dim startLog As TRowStatus = TRowStatus.rsNotIn
        Dim startBufferPoolAndMemory As TRowStatus = TRowStatus.rsNotIn
        Dim startRowOperations As TRowStatus = TRowStatus.rsNotIn

        For i As Integer = 0 To _RawResult.Count - 1
            If _RawResult(i).Length >= 3 AndAlso _RawResult(i).Substring(0, 3) = "---" Then
                Increment(startBackGroundThread)
                Increment(startSemaphores)
                Increment(startTransactions)
                Increment(startFileIO)
                Increment(startInsertBufferAndAdaptiveHashIndex)
                Increment(startLog)
                Increment(startBufferPoolAndMemory)
                Increment(startRowOperations)
            End If

            If _RawResult(i) = "BACKGROUND THREAD" Then
                startBackGroundThread = TRowStatus.rsIn1
                _BackGroundThread.Add(_RawResult(i - 1))
            End If
            If _RawResult(i) = "SEMAPHORES" Then
                startSemaphores = TRowStatus.rsIn1
                _Semaphores.Add(_RawResult(i - 1))
            End If

            If _RawResult(i) = "TRANSACTIONS" Then
                startTransactions = TRowStatus.rsIn1
                _Transactions.Add(_RawResult(i - 1))
            End If
            If _RawResult(i) = "FILE I/O" Then
                startFileIO = TRowStatus.rsIn1
                _FileIO.Add(_RawResult(i - 1))
            End If
            If _RawResult(i) = "INSERT BUFFER AND ADAPTIVE HASH INDEX" Then
                startInsertBufferAndAdaptiveHashIndex = TRowStatus.rsIn1
                _InsertBufferAndAdaptiveHashIndex.Add(_RawResult(i - 1))
            End If
            If _RawResult(i) = "LOG" Then
                startLog = TRowStatus.rsIn1
                _Log.Add(_RawResult(i - 1))
            End If
            If _RawResult(i) = "BUFFER POOL AND MEMORY" Then
                startBufferPoolAndMemory = TRowStatus.rsIn1
                _BufferPoolAndMemory.Add(_RawResult(i - 1))
            End If
            If _RawResult(i) = "ROW OPERATIONS" Then
                startRowOperations = TRowStatus.rsIn1
                _RowOperations.Add(_RawResult(i - 1))
            End If

            If startBackGroundThread = TRowStatus.rsIn1 OrElse startBackGroundThread = TRowStatus.rsIn2 Then
                _BackGroundThread.Add(_RawResult(i))
            End If
            If startSemaphores = TRowStatus.rsIn1 OrElse startSemaphores = TRowStatus.rsIn2 Then
                _Semaphores.Add(_RawResult(i))
            End If
            If startTransactions = TRowStatus.rsIn1 OrElse startTransactions = TRowStatus.rsIn2 Then
                _Transactions.Add(_RawResult(i))
            End If
            If startFileIO = TRowStatus.rsIn1 OrElse startFileIO = TRowStatus.rsIn2 Then
                _FileIO.Add(_RawResult(i))
            End If
            If startInsertBufferAndAdaptiveHashIndex = TRowStatus.rsIn1 OrElse startInsertBufferAndAdaptiveHashIndex = TRowStatus.rsIn2 Then
                _InsertBufferAndAdaptiveHashIndex.Add(_RawResult(i))
            End If
            If startLog = TRowStatus.rsIn1 OrElse startLog = TRowStatus.rsIn2 Then
                _Log.Add(_RawResult(i))
            End If
            If startBufferPoolAndMemory = TRowStatus.rsIn1 OrElse startBufferPoolAndMemory = TRowStatus.rsIn2 Then
                _BufferPoolAndMemory.Add(_RawResult(i))
            End If
            If startRowOperations = TRowStatus.rsIn1 OrElse startRowOperations = TRowStatus.rsIn2 Then
                _RowOperations.Add(_RawResult(i))
            End If
        Next
    End Sub

    Public ReadOnly Property Result() As String
        Get
            Dim s As New System.Text.StringBuilder
            For i As Integer = 0 To _RawResult.Count - 1
                s.AppendLine(_RawResult(i))
            Next
            Return s.ToString
        End Get
    End Property
    Public ReadOnly Property BackGroundThread() As String
        Get
            Dim s As New system.text.stringbuilder
            For i As Integer = 0 To _BackGroundThread.Count - 1
                s.AppendLine(_BackGroundThread(i))
            Next
            Return s.ToString
        End Get
    End Property

    Public ReadOnly Property Semaphores() As String
        Get
            Dim s As New system.text.stringbuilder
            For i As Integer = 0 To _Semaphores.Count - 1
                s.AppendLine(_Semaphores(i))
            Next
            Return s.ToString
        End Get
    End Property

    Public ReadOnly Property Transactions() As String
        Get
            Dim s As New system.text.stringbuilder
            For i As Integer = 0 To _Transactions.Count - 1
                s.AppendLine(_Transactions(i))
            Next
            Return s.ToString
        End Get
    End Property

    Public ReadOnly Property FileIO() As String
        Get
            Dim s As New system.text.stringbuilder
            For i As Integer = 0 To _FileIO.Count - 1
                s.AppendLine(_FileIO(i))
            Next
            Return s.ToString
        End Get
    End Property

    Public ReadOnly Property InsertBufferAndAdaptiveHashIndex() As String
        Get
            Dim s As New system.text.stringbuilder
            For i As Integer = 0 To _InsertBufferAndAdaptiveHashIndex.Count - 1
                s.AppendLine(_InsertBufferAndAdaptiveHashIndex(i))
            Next
            Return s.ToString
        End Get
    End Property

    Public ReadOnly Property Log() As String
        Get
            Dim s As New system.text.stringbuilder
            For i As Integer = 0 To _Log.Count - 1
                s.AppendLine(_Log(i))
            Next
            Return s.ToString
        End Get
    End Property

    Public ReadOnly Property BufferPoolAndMemory() As String
        Get
            Dim s As New system.text.stringbuilder
            For i As Integer = 0 To _BufferPoolAndMemory.Count - 1
                s.AppendLine(_BufferPoolAndMemory(i))
            Next
            Return s.ToString
        End Get
    End Property

    Public ReadOnly Property RowOperations() As String
        Get
            Dim s As New system.text.stringbuilder
            For i As Integer = 0 To _RowOperations.Count - 1
                s.AppendLine(_RowOperations(i))
            Next
            Return s.ToString
        End Get
    End Property

End Class
